package com.woniu.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;

/**
 * @author liangpei
 * @version 1.0
 * @description: excel解析类
 * @date 2024/3/22 17:46
 */
@Component
public class ParseExcel {

    public static <T> List<T> parseExcel(InputStream file, Class<T> clazz) {
        List<T> list = Lists.newArrayList();
        EasyExcel.read(file, clazz, new AnalysisEventListener<T>() {
            @Override
            public void invoke(T t, AnalysisContext context) {
                list.add(t);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 解析完成后，在这里处理 list，保存到数据库

            }
        }).sheet().doRead();
        return list;
    }
}
